package com.cheyunkeji.er.activity.evaluate;

import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.evaluate.EvaluateDetailResult2;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.http.RespCallback;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;
import com.cheyunkeji.er.view.evaluate.BCSMCheckItemView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;

public class DiPanCheckBCSM extends BaseActivity {


    private static final String TAG = DiPanCheckBCSM.class.getSimpleName();

    @BindView(R.id.v_topbar)
    TopBar vTopbar;
    @BindView(R.id.bcsm_155)
    BCSMCheckItemView bcsm155;
    @BindView(R.id.bcsm_156)
    BCSMCheckItemView bcsm156;
    @BindView(R.id.bcsm_157)
    BCSMCheckItemView bcsm157;
    @BindView(R.id.bcsm_158)
    BCSMCheckItemView bcsm158;
    @BindView(R.id.bcsm_159)
    BCSMCheckItemView bcsm159;
    @BindView(R.id.bcsm_160)
    BCSMCheckItemView bcsm160;
    @BindView(R.id.bcsm_161)
    BCSMCheckItemView bcsm161;
    @BindView(R.id.bcsm_162)
    BCSMCheckItemView bcsm162;
    @BindView(R.id.bcsm_163)
    BCSMCheckItemView bcsm163;
    @BindView(R.id.bcsm_164)
    BCSMCheckItemView bcsm164;
    @BindView(R.id.bcsm_165)
    BCSMCheckItemView bcsm165;
    @BindView(R.id.bcsm_166)
    BCSMCheckItemView bcsm166;
    @BindView(R.id.bcsm_167)
    BCSMCheckItemView bcsm167;
    @BindView(R.id.bcsm_168)
    BCSMCheckItemView bcsm168;
    @BindView(R.id.bcsm_169)
    BCSMCheckItemView bcsm169;
    @BindView(R.id.bcsm_170)
    BCSMCheckItemView bcsm170;
    @BindView(R.id.bcsm_171)
    BCSMCheckItemView bcsm171;
    @BindView(R.id.bcsm_172)
    BCSMCheckItemView bcsm172;
    @BindView(R.id.bcsm_173)
    BCSMCheckItemView bcsm173;
    @BindView(R.id.bcsm_174)
    BCSMCheckItemView bcsm174;
    @BindView(R.id.bcsm_175)
    BCSMCheckItemView bcsm175;
    @BindView(R.id.bcsm_176)
    BCSMCheckItemView bcsm176;
    @BindView(R.id.activity_di_pan_check_bcsm)
    LinearLayout activityDiPanCheckBcsm;

    private Map<Integer, BCSMCheckItemView> itemViewList = null;

    private List<EvaluateDetailResult2.DetectionBean.ChassisMarkBean> chassisMarkList = null;

    @Override
    protected void init() {

        setContentView(R.layout.er_activity_di_pan_check_bcsm);
        ButterKnife.bind(this);

        itemViewList = new HashMap<>();
        itemViewList.put(155, bcsm155);
        itemViewList.put(156, bcsm156);
        itemViewList.put(157, bcsm157);
        itemViewList.put(158, bcsm158);
        itemViewList.put(159, bcsm159);
        itemViewList.put(160, bcsm160);
        itemViewList.put(161, bcsm161);
        itemViewList.put(162, bcsm162);
        itemViewList.put(163, bcsm163);
        itemViewList.put(164, bcsm164);
        itemViewList.put(165, bcsm165);
        itemViewList.put(166, bcsm166);
        itemViewList.put(167, bcsm167);
        itemViewList.put(168, bcsm168);
        itemViewList.put(169, bcsm169);
        itemViewList.put(170, bcsm170);
        itemViewList.put(171, bcsm171);
        itemViewList.put(172, bcsm172);
        itemViewList.put(173, bcsm173);
        itemViewList.put(174, bcsm174);
        itemViewList.put(175, bcsm175);
        itemViewList.put(176, bcsm176);
    }

    @Override
    protected void initView() {

        vTopbar.setLeftBack();
        vTopbar.setRight("保存", -1, this);
        vTopbar.setTitle("底盘检查补充说明");

        if (null != MyApplication.getInstance().getEvaluateResult().getDetection() && null != MyApplication.getInstance().getEvaluateResult().getDetection().getChassis_mark()) {

            chassisMarkList = MyApplication.getInstance().getEvaluateResult().getDetection().getChassis_mark();

            if (chassisMarkList.size() > 0) {

                displayInfo();

            }
        }
    }

    private void displayInfo() {

        for (EvaluateDetailResult2.DetectionBean.ChassisMarkBean bean : chassisMarkList) {

            itemViewList.get(bean.getId()).setCheckedIndex(bean.getStatus() - 1);
            itemViewList.get(bean.getId()).setTag(bean);
        }

    }

    @Override
    protected void initData() {


    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        if (chassisMarkList == null) {
            chassisMarkList = new ArrayList<>();
        } else {
            chassisMarkList.clear();
        }

        if (v.getId() == R.id.vRight) {

            for (int index : itemViewList.keySet()) {
                if (3 != itemViewList.get(index).getCurrentCheckedIndex()) {
                    chassisMarkList.add(new EvaluateDetailResult2.DetectionBean.ChassisMarkBean(index, itemViewList.get(index).getCurrentCheckedIndex() + 1));
                }
            }

            String myData = new StringBuilder("{\"chassis_mark\":").append(new Gson().toJson(chassisMarkList)).append("}").toString();

            Log.e(TAG, "onClick: " + new Gson().toJson(chassisMarkList));

            HashMap<String, String> params = new HashMap<>();
//            params.put("authkey", UserInfoManager.getInstance(MyApplication.getInstance()).getAuthKey());
//            params.put(ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getYkey(), ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getY7e_e73a691f02a46500201c3998e6a2989d());
            params.put("aid", MyApplication.getInstance().getEvaluateResult().getAid());

            params.put("detection", myData);

            ApiClient.postForm(HttpConstants.INSERT_ARCHIVE_INFO, params, new RespCallback<EvaluateDetailResult2>() {

                @Override
                protected void onRequestFailed(String msg) {
                    SToast.show(msg);
                }

                @Override
                protected void onSuccess(EvaluateDetailResult2 data) {

                    if (null != data) {
                        MyApplication.getInstance().setEvaluateResult(data);
                        SToast.show("保存成功");
                        MyApplication.getInstance().removeActivity(DiPanCheckBCSM.class);
                        finish();
                    }
                }

                @Override
                public void onBefore(Request request, int id) {
                    showDialog();
                }

                @Override
                public void onAfter(int id) {
                    dismissDialog();
                }

            });

        }

    }
}
